package cz.czu.pef.DataModelling.controller;

import java.util.*;

import javax.faces.bean.SessionScoped;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cz.czu.pef.DataModelling.Person;
import cz.czu.pef.DataModelling.ejb.api.AbstractWrapperService;

@Controller
@SessionScoped
public class DMController
{
	@Autowired private AbstractWrapperService<Person> personServiceEJB;

	public List<Person> getAllPeople()
	{
		return personServiceEJB.findAll();
	}

	@RequestMapping("/peopleList")
	public String listPeople(Map<String, Object> map) {

		map.put("peopleList", personServiceEJB.findAll());

		return "peopleList";
	}

	@RequestMapping(value = "/addPerson", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("person") Person person, BindingResult result) {
		personServiceEJB.merge(person);

		return "redirect:/index";
	}

	@RequestMapping("/deletePerson/{personId}")
	public String deleteContact(@PathVariable("personId") Long personId) {
		personServiceEJB.deleteById(personId + "");

		return "redirect:/index";
	}
}
